Skip to content

Conversation

@TApplencourt
Copy link
Contributor

@TApplencourt TApplencourt commented Dec 1, 2025

Close #167421

This PR adds the environment variables LIBCLANG_LIBRARY_PATH and LIBCLANG_LIBRARY_FILE, which allow users to specify the directory path and the exact library file that should be used to locate libclang.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:as-a-library libclang and C++ API labels Dec 1, 2025
@llvmbot
Copy link
Member

llvmbot commented Dec 1, 2025

@llvm/pr-subscribers-clang

Author: Thomas Applencourt (TApplencourt)

Changes

Close #167421

This PR ad the environment variables LIBCLANG_LIBRARY_PATH and LIBCLANG_LIBRARY_FILE, which allow users to specify the directory path and the exact library file that should be used to locate libclang.


Full diff: https://github.com/llvm/llvm-project/pull/170201.diff

2 Files Affected:

  • (modified) clang/bindings/python/clang/cindex.py (+2-2)
  • (modified) clang/docs/ReleaseNotes.rst (+3)
diff --git a/clang/bindings/python/clang/cindex.py b/clang/bindings/python/clang/cindex.py
index d352373e85c60..b728a8d8369ad 100644
--- a/clang/bindings/python/clang/cindex.py
+++ b/clang/bindings/python/clang/cindex.py
@@ -4383,8 +4383,8 @@ def register(item: LibFunc) -> None:
 
 
 class Config:
-    library_path = None
-    library_file: str | None = None
+    library_path: str | None = os.environ.get("LIBCLANG_LIBRARY_PATH")
+    library_file: str | None = os.environ.get("LIBCLANG_LIBRARY_FILE")
     compatibility_check = True
     loaded = False
 
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 9f8d781c93021..fb812a21f2f31 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -170,6 +170,9 @@ Clang Python Bindings Potentially Breaking Changes
   ElaboratedTypes. The value becomes unused, and all the existing users should
   expect the former underlying type to be reported instead.
 - Remove ``AccessSpecifier.NONE`` kind. No libclang interfaces ever returned this kind.
+- Added the environment variables ``LIBCLANG_LIBRARY_PATH`` and ``LIBCLANG_LIBRARY_FILE``,
+  which allow users to specify the directory path and the exact library file that
+  should be used to locate libclang.
 
 What's New in Clang |release|?
 ==============================

@TApplencourt
Copy link
Contributor Author

TApplencourt commented Dec 1, 2025

For more rational on the change see the issue #167421.

The PR is currently in the "minimal" stage. I can:

  • In the test remove the old support for CLANG_LIBRARY_PATH, and add in the release note that people should migrate to LIBCLANG_LIBRARY_PATH (will remove 3 lines per tests file so simplifying the code base)
  • Add tests. The simple one will be to do a negative testing (aka setting LIBCLANG_LIBRARY_PATH to /dev/null and verify that it crash). Other test seem harder (1/ verify that cindex if working 2/ find the libclang.so that he used, 3/ use this path to set LIBCLANG_LIBRARY_PATH, retry)

Curious what do people thinks (previous CLANG_LIBRARY_PATH didn't get any test, but it was used only the tests)

Copy link
Contributor

@DeinAlptraum DeinAlptraum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add in the release note that people should migrate to LIBCLANG_LIBRARY_PATH

I don't think that's necessary since this is only used by the tests and they are not part of the interface we offer.
Where this should be adapted however is the clang/bindings/python/README.md

Add tests.

Yes please. You can create an additional test file for this, where you set the environment variable from the code, and then import cindex. Then this shouldn't be an issue. I think...
All in favor of removing the old setting CLANG_LIBRARY_PATH at the top of all the test files. Note that the current cmake build flow requires this, so you will need to change the variable name in clang/bindings/python/tests/CMakeLists.txt

Additionally, in get_cindex_library, an error message is returned that refers to the methods to set the library file. This should be adapted to also mention the environment variables.

@DeinAlptraum
Copy link
Contributor

CC @Endilll

@github-actions
Copy link

github-actions bot commented Dec 3, 2025

✅ With the latest revision this PR passed the Python code formatter.

@TApplencourt
Copy link
Contributor Author

TApplencourt commented Dec 3, 2025

Ok, so:

  • Added new tests: clang/bindings/python/tests/cindex/test_environment_variable.py
  • Removed the old environments notions (update the README and the CINDEX error message)
  • Updated the error message

Sorry I failed my git amend so the commit history is ugly, will make the review harder.


Erf, I run black I guess darker is not black : ( Will fix!

@TApplencourt TApplencourt force-pushed the env_cindex branch 2 times, most recently from 9deaa3f to 6e158e8 Compare December 3, 2025 18:56
Copy link
Contributor

@DeinAlptraum DeinAlptraum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some minor cleanup, otherwise LGTM!

@Endilll Endilll self-requested a review December 4, 2025 17:04
Copy link
Contributor

@Endilll Endilll left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a fan of the existing terminology in this part of our codebase, where "path" should be understood as "search path" or "containing directory", while "file" and "filename" are "path to the file" (probably a full path, but that is yet to be confirmed). I left couple of suggestions that should improve the clarity of the error message and the release note.

That's probably out of scope here, but I'd like to see Config.library_path, Config.library_file, Config.get_filename to be renamed to something less confusing. As for LIBCLANG_LIBRARY_PATH, it can be argued that it follows a well-established example of LD_LIBRARY_PATH, so I can live with this.

@TApplencourt
Copy link
Contributor Author

I'm not a fan of the existing terminology in this part of our codebase, where "path" should be understood as "search path" or "containing directory", while "file" and "filename" are "path to the file" (probably a full path, but that is yet to be confirmed).
That's probably out of scope here, but I'd like to see Config.library_path, Config.library_file, Config.get_filename to be renamed to something less confusing.

Totally agree! I just followed the name of the Config.set_* function for the env, but I find it confusing too! And finding good name is hard, I never really find good guideline of the naming of path.

Thanks for the comment, far better indeed. Merged them (in N commit because I don't know how to use github UI...)

@Endilll
Copy link
Contributor

Endilll commented Dec 4, 2025

Thanks for the comment, far better indeed. Merged them (in N commit because I don't know how to use github UI...)

I don't think there is a way to batch applications of suggested changes, so no worries here.
You might need to reflow the text, though.

@TApplencourt
Copy link
Contributor Author

You might need to reflow the text, though.

Didn't seem to be required. We got lucky : ) I did added a the in you can specify the path (I checked with my native speaker office-mate, " the “the” article is needed since path is singular " 🤷🏽)

Thanks for reviews @Endilll and @DeinAlptraum!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:as-a-library libclang and C++ API clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[libclang/python] Add ENV variable for library_{path,file}

4 participants